
PDShortcut = function(elementId, containerElementId) {
    this.elementId = elementId;
    this.containerElementId = containerElementId;
    this.containerComponentElementId = EchoDomUtil.getComponentId(containerElementId);
    
    this.dragOriginX = null;
    this.dragOriginY = null;
    this.dragInitPositionX = null;
    this.dragInitPositionY = null;
    this.dragInitWidth = null;
    this.dragInitHeight = null;
    
    this.icon = null;
    this.height = PDShortcut.DEFAULT_HEIGHT;
    this.width = PDShortcut.DEFAULT_WIDTH;
    this.overflow = "auto";
    this.positionX = null;
    this.positionY = null;
    this.title = null;    
	this.firstClickTime = null;
};

PDShortcut.activeInstance = null;

PDShortcut.DEFAULT_WIDTH = 48;
PDShortcut.DEFAULT_HEIGHT = 60;

PDShortcut.prototype.create = function() {
    var containerElement = document.getElementById(this.containerElementId);
    
    var shortcutDivElement = document.createElement("div");
    shortcutDivElement.id = this.elementId;
    shortcutDivElement.style.position = "absolute";
    shortcutDivElement.style.zIndex = 0;
    
    if (this.positionX == null) {
		this.positionX = 200;
	}
    if (this.positionY == null) {
    	this.positionY = 200;
    }
    shortcutDivElement.style.left = this.positionX + "px";
    shortcutDivElement.style.top = this.positionY + "px";
    shortcutDivElement.style.width = this.width + "px";
    shortcutDivElement.style.height = this.height + "px";
    
	//the icon
    var iconDivElement = document.createElement("div");
	iconDivElement.id = this.elementId + "_icon";
    iconDivElement.style.cursor = "pointer";
    var closeImgElement = document.createElement("img");
    closeImgElement.setAttribute("src", this.icon);
    iconDivElement.appendChild(closeImgElement);
    iconDivElement.style.marginLeft = "16px";    
    shortcutDivElement.appendChild(iconDivElement);

	//the text
    var textElement = document.createElement("div");
	textElement.id = this.elementId + "_text";
    textElement.style.verticalAlign = "bottom";
   	textElement.style.textAlign = "center";
    textElement.style.width = "60px";    
    textElement.style.fontSize = "9px";
    textElement.appendChild(document.createTextNode(this.title));
    
    shortcutDivElement.appendChild(textElement);
    
    containerElement.appendChild(shortcutDivElement);

    EchoDomPropertyStore.setPropertyValue(shortcutDivElement, "component", this);
    EchoEventProcessor.addHandler(shortcutDivElement, "mousedown", "PDShortcut.processMouseDown");
    EchoEventProcessor.addHandler(shortcutDivElement, "click", "PDShortcut.processAction");
    
};

PDShortcut.prototype.dispose = function() {
    this.removeListeners();

    var shortcutDivElement = document.getElementById(this.elementId);

    EchoEventProcessor.removeHandler(shortcutDivElement, "mousedown");
    EchoEventProcessor.removeHandler(shortcutDivElement, "click");
    
    EchoDomPropertyStore.dispose(this.elementId);
};

PDShortcut.prototype.processAction = function(echoEvent) {
	if (!EchoClientEngine.verifyInput(this.elementId)) {
        return;
    }
		
    var d = new Date();
   	var now = d.getTime();
    if ((now - this.firstClickTime) < 350) {  //350ms timeout
        //double click       
	    EchoClientMessage.setActionValue(this.elementId, "action");
    	EchoServerTransaction.connect();
    } else {
       //single click
		this.firstClickTime = d.getTime();
	}	
};

PDShortcut.prototype.processMouseDown = function(echoEvent) {

    if (!EchoClientEngine.verifyInput(this.elementId)) {
		return;
    }
    
    EchoDomUtil.preventEventDefault(echoEvent);
    PDShortcut.activeInstance = this;
    this.dragInitPositionX = this.positionX;
    this.dragInitPositionY = this.positionY;
    this.dragOriginX = echoEvent.clientX;
    this.dragOriginY = echoEvent.clientY;
    
    // Remove all listeners to avoid possible retention issues in IE.
    this.removeListeners();
    
    EchoDomUtil.addEventListener(document, "mousemove", PDShortcut.processMouseMove, false);
    EchoDomUtil.addEventListener(document, "mouseup", PDShortcut.processMouseUp, false);
    
    if (EchoClientProperties.get("browserInternetExplorer")) {
        EchoDomUtil.addEventListener(document, "selectstart", PDShortcut.selectStart, false);
     }
};

PDShortcut.prototype.processMouseMove = function(e) {

	var posX = this.dragInitPositionX + e.clientX - this.dragOriginX;
	var posY = this.dragInitPositionY + e.clientY - this.dragOriginY;
	
    this.setPosition(posX, posY);
    
	var shortcutDivElement = document.getElementById(this.elementId);
    if (posX < 40 && posY < 68) {  //TODO position of recycle bin is hardcoded, use relative position
    	shortcutDivElement.style.opacity = '0.5';
    	shortcutDivElement.style.backgroundColor = "#005faf";
    } else {
    	shortcutDivElement.style.opacity = '0.9';
    	shortcutDivElement.style.backgroundColor = null;
    }       
    this.redraw();
};

PDShortcut.prototype.processMouseUp = function(e) {
    this.removeListeners();
    PDShortcut.activeInstance = null;
	EchoClientMessage.setPropertyValue(this.elementId, "positionX", this.positionX + "px");
	EchoClientMessage.setPropertyValue(this.elementId, "positionY", this.positionY + "px");
	if (this.positionX < 40 && this.positionY < 68) {  //TODO positions are hardcoded, use relative position
		var shortcutDivElement = document.getElementById(this.elementId);
    	shortcutDivElement.style.opacity = '0.3';
    	shortcutDivElement.style.backgroundColor = null;
    }
   	var doActionDelayed = function() {
		EchoServerTransaction.connect();
	}; 
  	window.setTimeout(doActionDelayed, 500);  //delay, otherwise double click will not work properly 
};

PDShortcut.prototype.redraw = function() {
    var shortcutDivElement = document.getElementById(this.elementId);

    shortcutDivElement.style.left = this.positionX + "px";
    shortcutDivElement.style.top = this.positionY + "px";
    shortcutDivElement.style.width = this.width + "px";
    shortcutDivElement.style.height = this.height + "px";
};

PDShortcut.prototype.removeListeners = function() {
    EchoDomUtil.removeEventListener(document, "mousemove", PDShortcut.processMouseMove, false);
    EchoDomUtil.removeEventListener(document, "mouseup", PDShortcut.processMouseUp, false);
};

PDShortcut.prototype.setPosition = function(positionX, positionY) {
    this.positionX = positionX;
    this.positionY = positionY;
};

PDShortcut.getComponent = function(element) {
    return EchoDomPropertyStore.getPropertyValue(element, "component");
};

/**
 * Do-nothing event handler.
 */
PDShortcut.nullEventHandler = function(echoEvent) { };

PDShortcut.processAction = function(echoEvent) {
    var componentId = EchoDomUtil.getComponentId(echoEvent.registeredTarget.id);
    var shortcut = PDShortcut.getComponent(componentId);
    shortcut.processAction(echoEvent);
};

/**
 * Event handler for "SelectStart" events to disable selection while dragging
 * the Window.  (Internet Explorer specific)
 */
PDShortcut.selectStart = function() {
    EchoDomUtil.preventEventDefault(window.event);
};

PDShortcut.processMouseDown = function(echoEvent) {
    var componentId = EchoDomUtil.getComponentId(echoEvent.registeredTarget.id);
    var shortcut = PDShortcut.getComponent(componentId);
    shortcut.processMouseDown(echoEvent);
};

PDShortcut.processMouseMove = function(e) {
    e = e ? e : window.event;
    if (PDShortcut.activeInstance) {
        PDShortcut.activeInstance.processMouseMove(e);
    }
};

PDShortcut.processMouseUp = function(e) {
    e = e ? e : window.event;
    if (PDShortcut.activeInstance) {
        PDShortcut.activeInstance.processMouseUp(e);
    }
};


PDShortcut.MessageProcessor = function() { };

PDShortcut.MessageProcessor.loadProperties = function(propertiesElement, shortcut) {
    if (propertiesElement.getAttribute("position-x")) {
        shortcut.positionX = parseInt(propertiesElement.getAttribute("position-x"));
    }
    if (propertiesElement.getAttribute("position-y")) {
        shortcut.positionY = parseInt(propertiesElement.getAttribute("position-y"));
    }
    if (propertiesElement.getAttribute("title")) {
        shortcut.title = propertiesElement.getAttribute("title");
    }
};

PDShortcut.MessageProcessor.process = function(messagePartElement) {
    for (var i = 0; i < messagePartElement.childNodes.length; ++i) {
        if (messagePartElement.childNodes[i].nodeType == 1) {
            switch (messagePartElement.childNodes[i].tagName) {
            case "dispose":
                PDShortcut.MessageProcessor.processDispose(messagePartElement.childNodes[i]);
                break;
            case "init":
                PDShortcut.MessageProcessor.processInit(messagePartElement.childNodes[i]);
                break;
            case "update":
                PDShortcut.MessageProcessor.processUpdate(messagePartElement.childNodes[i]);
                break;
            }
        }
    }
};

PDShortcut.MessageProcessor.processDispose = function(disposeElement) {
    var elementId = disposeElement.getAttribute("eid");
    var shortcut = PDShortcut.getComponent(elementId);
    if (shortcut) {
        shortcut.dispose();
    }
};

PDShortcut.MessageProcessor.processUpdate = function(updateElement) {
    var elementId = updateElement.getAttribute("eid");
    var shortcut = PDShortcut.getComponent(elementId);
    if (!shortcut) {
        throw new Error("No PDShortcut with id: " + elementId);
    }
    PDShortcut.MessageProcessor.loadProperties(updateElement, shortcut);
    shortcut.redraw();
};

PDShortcut.MessageProcessor.processInit = function(initElement) {
    var elementId = initElement.getAttribute("eid");
    var containerElementId = initElement.getAttribute("container-eid");
    var shortcut = new PDShortcut(elementId, containerElementId);
    
    PDShortcut.MessageProcessor.loadProperties(initElement, shortcut);
    if (initElement.getAttribute("icon")) {
        shortcut.icon = initElement.getAttribute("icon");
    }
    if (initElement.getAttribute("title")) {
        shortcut.title = initElement.getAttribute("title");
    }
    
    shortcut.create();
};